using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace BasicGridCollisionDemo
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        int currentId;

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Grid grid;

        CellObject playerObject;
        CellObject[] objects;


        const int NUM_OBJECTS = 10;

        Texture2D blankTexture;

        KeyboardState currentKeyboardState;
        KeyboardState previousKeyboardState;

        MouseState currentMouseState;
        MouseState previousMouseState;


        BasicEffect basicEffect;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            currentKeyboardState = Keyboard.GetState();
            currentMouseState = Mouse.GetState();

            graphics.PreferredBackBufferWidth = 512;
            graphics.PreferredBackBufferHeight = 512;

        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            grid = new Grid(16, 16, 32,32);

            blankTexture = new Texture2D(GraphicsDevice, 1, 1);
            Color[] colors = new Color[1];
            colors[0] = Color.White;
            blankTexture.SetData(colors);

            basicEffect = new BasicEffect(GraphicsDevice);
            basicEffect.VertexColorEnabled = true;
            
            objects = new CellObject[1];

            playerObject = new CellObject(currentId, grid, new Vector2(200, 200), new Vector2(15), Color.White);
            objects[0] = playerObject;

            DebugDrawer.LoadContent(GraphicsDevice);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // TODO: Add your update logic here
            UpdateInput(gameTime);

            base.Update(gameTime);
        }

        protected void UpdateInput(GameTime gameTime)
        {
            previousKeyboardState = currentKeyboardState;
            currentKeyboardState = Keyboard.GetState();

            previousMouseState = currentMouseState;
            currentMouseState = Mouse.GetState();

            //Esc exists the game
            if (currentKeyboardState.IsKeyDown(Keys.Escape) && previousKeyboardState.IsKeyUp(Keys.Escape))
                Exit();

            //directional bottons move the object
            int x = 0, y = 0;
            if (currentKeyboardState.IsKeyDown(Keys.Right))
            {
                x = 3;
            }
            else if (currentKeyboardState.IsKeyDown(Keys.Left))
            {
                x = -3;
            }


            if (currentKeyboardState.IsKeyDown(Keys.Down))
            {
                y = 3;
            }
            else if (currentKeyboardState.IsKeyDown(Keys.Up))
            {
                y = -3;
            }

            playerObject.Move(new Vector2(x, y));


            //a/s & z/x  increase/decrease the size of the object
            int sizeX = 0, sizeY = 0;

            if (currentKeyboardState.IsKeyDown(Keys.A))
            {
                sizeX = 3;
            }
            else if (currentKeyboardState.IsKeyDown(Keys.S))
            {
                sizeX = -3;
            }


            if (currentKeyboardState.IsKeyDown(Keys.Z))
            {
                sizeY = 3;
            }
            else if (currentKeyboardState.IsKeyDown(Keys.X))
            {
                sizeY = -3;
            }

            playerObject.ChangeSize(new Vector2(sizeX, sizeY));

        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();

            

            //Draw the Grid
            DrawGrid();

            //Draw the objects
            foreach (CellObject cellObject in objects)
            {
                cellObject.Draw(spriteBatch, gameTime, blankTexture);
            }

            spriteBatch.End();
            base.Draw(gameTime);
        }

        public void DrawGrid()
        {
            basicEffect.Techniques[0].Passes[0].Apply();
            
            //draw non empty cells
            for (int i = 0; i < grid.Cells.Length; i++)
            {
                List<int> cell = grid.Cells[i];

                if (cell.Count > 0)
                {
                    int x = i % grid.NColumns * grid.CellWidth;
                    int y = i / grid.NLines * grid.CellHeight;

                    Rectangle rect = new Rectangle(x, y, grid.CellWidth, grid.CellHeight);
                    spriteBatch.DrawSolidRectangle(rect, Color.Red);
                }
            }

            //Draw the lines
            for(int i =0; i< grid.NLines; i++)
            {
                int line = i*grid.CellHeight;
                int start = 0;
                int lenght = grid.CellWidth*grid.NColumns;
                spriteBatch.DrawLineSegment(new Vector2(start, line), new Vector2(lenght, line), Color.Yellow, 1);
            }

            //draw the cells);
            for (int i = 0; i < grid.NLines; i++)
            {
                int column = i * grid.CellWidth;
                int start = 0;
                int lenght = grid.CellHeight * grid.NLines;
                spriteBatch.DrawLineSegment(new Vector2(column, start), new Vector2(column, lenght), Color.Yellow, 1);
            }

            
        }

    }
}
